//
//  RichTextFormatToolbarBase.swift
//  RichTextKit
//
//  Created by Daniel Saidi on 2024-02-16.
//  Copyright © 2024 Daniel Saidi. All rights reserved.
//

#if iOS || macOS || os(visionOS)
import SwiftUI

/// This internal protocol is used to share code between the
/// two toolbars, which should eventually become one.
protocol RichTextFormatToolbarBase: View {
    var config: RichTextFormat.ToolbarConfig { get }
    var style: RichTextFormat.ToolbarStyle { get }
}

extension RichTextFormatToolbarBase {

    var hasColorPickers: Bool {
        let colors = config.colorPickers
        let disclosed = config.colorPickersDisclosed
        return !colors.isEmpty || !disclosed.isEmpty
    }
}

public struct fuckColorPicker: View {
    @ObservedObject var context: RichTextContext
    
    @State private var colorFor: RichTextColor = .foreground
    
    @Environment(\.richTextFormatSheetConfig)
    var config

    @Environment(\.richTextFormatSheetStyle)
    var style
    
    public init(context: RichTextContext) {
        self.context = context
    }
    public var body: some View {
        HStack {
            Picker("", selection: $colorFor) {
                ForEach(config.colorPickers) { color in
                    Text(text(color: color))
                        .tag(color)
                }
            }
            Spacer()
            colorPicker(for: colorFor, context: context)
        }
        .padding(.horizontal, style.padding)
    }
    
    func text(color: RichTextColor) -> String {
        switch color {
        case .foreground:
            return "字体颜色"
        case .background:
            return "背景颜色"
        case .strikethrough:
            return "删除线颜色"
        case .stroke:
            return "笔画颜色"
        case .underline:
            return "下划线颜色"
        }
    }
    @State var showColorPicker = false
    
    func colorPicker(
        for color: RichTextColor,
        context: RichTextContext
    ) -> some View {
        Button {
            showColorPicker = true
        } label: {
            Color(context.color(for: color) ?? .textColor)
                .frame(width: 48, height: 32)
                .clipShape(Capsule(style: .continuous))
        }
        .Popover(isPresented: $showColorPicker) {
            ColorPannicView(DefColor: context.binding(for: color))
        }
    }
}

extension RichTextFormatToolbarBase {

    @ViewBuilder
    func alignmentPicker(
        value: Binding<RichTextAlignment>
    ) -> some View {
        if !config.alignments.isEmpty {
            RichTextAlignment.Picker(
                selection: value,
                values: config.alignments
            )
            .pickerStyle(.segmented)
        }
    }

    @ViewBuilder
    func fontPicker(
        value: Binding<String>
    ) -> some View {
        
        if config.fontPicker {
            FontPicker(selection: value)
//            RichTextFont.Picker(
//                selection: value
//            )
//            .richTextFontPickerConfig(.init(fontSize: 12))
        }
    }

    @ViewBuilder
    func fontSizePicker(
        for context: RichTextContext
    ) -> some View {
        if config.fontSizePicker {
            RichTextFont.SizePickerStack(context: context)
                .buttonStyle(.bordered)
        }
    }

    @ViewBuilder
    func indentButtons(
        for context: RichTextContext,
        greedy: Bool
    ) -> some View {
        if config.indentButtons {
            RichTextAction.ButtonGroup(
                context: context,
                actions: [.stepIndent(points: -30), .stepIndent(points: 30)],
                greedy: greedy
            )
        }
    }

    @ViewBuilder
    func lineSpacingPicker(
        for context: RichTextContext
    ) -> some View {
        if config.lineSpacingPicker {
            RichTextLine.SpacingPickerStack(context: context)
                .buttonStyle(.bordered)
        }
    }

    @ViewBuilder
    func styleToggleGroup(
        for context: RichTextContext
    ) -> some View {
        if !config.styles.isEmpty {
            RichTextStyle.ToggleGroup(
                context: context,
                styles: config.styles
            )
        }
    }

    @ViewBuilder
    func superscriptButtons(
        for context: RichTextContext,
        greedy: Bool
    ) -> some View {
        if config.superscriptButtons {
            RichTextAction.ButtonGroup(
                context: context,
                actions: [.stepSuperscript(steps: -1), .stepSuperscript(steps: 1)],
                greedy: greedy
            )
        }
    }
}
#endif

public struct FontPicker: View {
    @Binding private var selection: String
    
    @State var showFontPicker: Bool = false
    
    public init(selection: Binding<String>) {
        self._selection = selection
    }
    
    public var body: some View {
        Button {
            showFontPicker = true
        } label: {
            Text(selection)
        }
        .sheet(isPresented: $showFontPicker) {
            UIFontPicker(selection: $selection)
        }
    }
}

public struct UIFontPicker: UIViewControllerRepresentable {
    @Binding private var selection: String
    @Environment(\.glazedDismiss) var glazedDismiss
    public init(selection: Binding<String>) {
        self._selection = selection
    }
    public func makeUIViewController(context: Context) -> UIFontPickerViewController {
        let fontConfig = UIFontPickerViewController.Configuration()
        fontConfig.includeFaces = true
        fontConfig.filteredTraits = []
        let fontPicker = UIFontPickerViewController(configuration: fontConfig)
        fontPicker.delegate = context.coordinator
        return fontPicker
    }
    
    public func updateUIViewController(_ uiViewController: UIFontPickerViewController, context: Context) {
        
    }
    
    public typealias UIViewControllerType = UIFontPickerViewController
    
    public func makeCoordinator() -> coordinator {
        coordinator(RootView: self)
    }
    public class coordinator: NSObject, UIFontPickerViewControllerDelegate {
        public let RootView: UIFontPicker
        
        public init(RootView: UIFontPicker) {
            self.RootView = RootView
        }
        
        public func fontPickerViewControllerDidPickFont(_ viewController: UIFontPickerViewController) {
            RootView.selection = viewController.selectedFontDescriptor?.postscriptName ?? "SongTi SC"
            RootView.glazedDismiss()
        }
    }
}
